{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6995fa00-bbdf-4c78-a67c-830bfd453e2a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "31415926535897"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "from numpy.random import default_rng\n",
    "\n",
    "SEED = int(10 ** 13 * np.pi)\n",
    "SEED"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ece1d2fd-c9dc-4592-a6c6-743188552db5",
   "metadata": {},
   "source": [
    "# Exercise 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b41eca4e-c070-4419-97a2-70f5745e34b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1.12087147, -1.47693615,  0.36959407,  1.52005265,  0.35310357,\n",
       "       -1.13555464, -1.63611594, -0.07295773, -1.06056688, -1.54483307])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gen = default_rng(SEED)\n",
    "gen.standard_normal(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0995d050-e49a-4ea1-9146-cefd468de0ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.34641686,  0.26624936, -0.04157256, -0.15597542, -0.71199179,\n",
       "        1.42614287,  0.59999017, -1.12868585,  2.45538905, -0.11073076])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gen.normal(size=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d7a4a952-d18f-41b7-a35d-ba6d7092a401",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 5.44821074,  4.91389961, -2.17536119,  1.66811946, -0.71530719,\n",
       "        4.8467949 ,  1.73302988,  4.62500782,  0.59214696, -2.05970507])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Parameters are mean and scale, i.e., stdandard deviation\n",
    "gen.normal(3, 3, size=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5d91fb8b-2d32-48d6-bd9a-ad9f2fb4c94f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.50589382, 0.74331811, 0.84894578, 0.78031166, 0.19871486,\n",
       "       0.75741724, 0.791959  , 0.70253428, 0.087387  , 0.8514891 ])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gen.random(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d2f3db94-69ca-49d2-b486-675869821598",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.30078457, 0.98701999, 0.76811124, 0.29045321, 0.45955654,\n",
       "       0.44255291, 0.14077977, 0.82417729, 0.64695318, 0.91361973])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gen.uniform(0, 1, size=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "00f48b7f-b8e0-4e05-adc9-e67ff9db5835",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.60869667, -0.21666756,  0.01584656,  0.73490492,  0.66487271,\n",
       "        0.78096438,  0.16203263, -0.64234756,  0.27588701,  0.79404433])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gen.uniform(-1, 1, size=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5f9edead-ec8d-4aaf-a99e-d7eef51b34e0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.46181776, 3.75753353, 5.77428015, 4.9774197 , 0.62712234,\n",
       "       0.08553175, 0.28261634, 3.67809018, 1.76750853, 1.7047245 ])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gen.gamma(1, 2, size=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "98b4a05a-b1fa-410d-bf01-563d82e0f0b2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2.13230629, 2.44962117, 1.99838733, 2.09064797, 1.88840364,\n",
       "       1.82857597, 1.92078925, 2.3497724 , 2.15522394, 2.43655622])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# mu, sigma\n",
    "gen.lognormal(0.8, 0.2, size=10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f4ebb3a-6b35-4e65-95c4-18c3438e8fa5",
   "metadata": {},
   "source": [
    "# Exercise 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8a07199f-1eb7-4926-9751-e8c694b0b5ba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 9,  3, 10,  4,  9,  1,  1,  2,  0,  7], dtype=int64)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gen = default_rng(SEED)\n",
    "first = gen.integers(0, 11, size=10)\n",
    "first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "78456772-f88e-420a-8475-4a9b0ae63689",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 9,  3, 10,  4,  9,  1,  1,  2,  0,  7], dtype=int64)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gen = default_rng(SEED)\n",
    "second = gen.integers(0, 11, size=10)\n",
    "second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c8ecf58f-b166-4e48-b61a-80153cfda02c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int64)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first - second"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d683305d-7862-4f36-a6bb-a6935be54f6a",
   "metadata": {},
   "source": [
    "# Exercise 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c1d60f4d-9f5b-483a-aedd-d8b311b945e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.99797994, -0.51077754],\n",
       "       [-0.51077754,  1.02748402]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sigma = np.array([[1, -0.5], [-0.5, 1]])\n",
    "mvn = gen.multivariate_normal([0, 0], sigma, size=10000)\n",
    "cov = np.cov(mvn.T)\n",
    "cov"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ad09a795-f193-4788-995e-7690f3d6d8e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.00202006, -0.01077754],\n",
       "       [-0.01077754,  0.02748402]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cov - sigma"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c39b3843-c478-4d5b-b806-b20cd9058f94",
   "metadata": {},
   "source": [
    "# Exercise 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "24ace678-8aec-4ffc-b252-669649dd34f5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.33966653, 0.3724006 , 0.10559387, 0.23378977, 0.65711565])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from numpy.random import PCG64, Generator, SeedSequence\n",
    "\n",
    "ss = SeedSequence(SEED)\n",
    "pcg = PCG64(ss)\n",
    "gen = Generator(pcg)\n",
    "pcg5 = gen.random(5)\n",
    "pcg5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "f40a79fd-994e-40ad-bcbd-37a516a67bb8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.33966653, 0.3724006 , 0.10559387, 0.23378977, 0.65711565])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gen = default_rng(SEED)\n",
    "gen.random(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "b3d5be77-1b8b-4a44-b02d-c4eb0f1a957c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from numpy.random import Philox\n",
    "\n",
    "ss = SeedSequence(SEED)\n",
    "philox = Philox(ss)\n",
    "philox_gen = Generator(philox)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "0958eba4-2395-402c-b13a-aebee91562d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.68144335, 0.17674075, 0.24246577, 0.21657299, 0.90329184])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "philox5 = philox_gen.random(5)\n",
    "philox5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "32455d1f-721e-4799-bf72-70c0f95800b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.34177682,  0.19565985, -0.1368719 ,  0.01721678, -0.24617619])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pcg5 - philox5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ce56ef0-d60a-40a1-9fc0-c02c5cac5f7d",
   "metadata": {},
   "source": [
    "# Exercise 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "073e0ccc-cc1b-4d4c-8932-ebaa9d856e4e",
   "metadata": {},
   "outputs": [],
   "source": [
    "ss = SeedSequence(SEED)\n",
    "children = ss.spawn(2)\n",
    "gen = default_rng(ss)\n",
    "child_gen1 = default_rng(children[0])\n",
    "child_gen2 = default_rng(children[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "ed7ed1da-c276-432e-8a48-ed03748d334f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SeedSequence(\n",
       "    entropy=31415926535897,\n",
       "    n_children_spawned=2,\n",
       ")"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# No spawn key\n",
    "ss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "8b5167a8-91fa-4bbd-a41f-b7e15e5c6861",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SeedSequence(\n",
       "    entropy=31415926535897,\n",
       "    spawn_key=(0,),\n",
       ")"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# See the spawn key\n",
    "children[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "ffa55e0d-0804-4267-a1cd-20a86c9e77d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SeedSequence(\n",
       "    entropy=31415926535897,\n",
       "    spawn_key=(1,),\n",
       ")"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Different spawn key\n",
    "children[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "f7972cda-16a4-420e-9f7a-1992d35ef4b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.33966653, 0.3724006 , 0.10559387, 0.23378977, 0.65711565])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gen.random(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "9b2b1c32-0bf3-4817-a312-991844d1ff29",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.10906813, 0.91112154, 0.80902991, 0.08099842, 0.82565723])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "child_gen1.random(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "f0057630-bbb2-435f-a88b-7ef5826f7c0f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.09535427, 0.69865339, 0.95871024, 0.97860932, 0.59972837])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "child_gen2.random(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1571117b-445d-4dd8-9ad8-759fdf5172ee",
   "metadata": {},
   "source": [
    "# Exercise 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "9fb926fa-2367-4663-aabb-97329b2e7c7d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.81792331, 0.5510463 , 0.41977535, 0.09869185, 0.81102075])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.seed(int(10 ** 5 * np.pi))\n",
    "np.random.random_sample(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "86fa5122-0897-472b-86e3-c764cef9eb23",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.81792331, 0.5510463 , 0.41977535, 0.09869185, 0.81102075])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.seed(int(10 ** 5 * np.pi))\n",
    "np.random.random_sample(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14ee9797-3af8-4bdc-9721-87bb95fba41d",
   "metadata": {},
   "source": [
    "# Exercise 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "3ecea548-dee6-44a1-9352-54e2ec68268e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.9673564 , 0.09820669, 0.80186037, 0.60490212, 0.58476881])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state = np.random.get_state()\n",
    "np.random.random_sample(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "2b463faf-561f-4d71-b522-dd0733ab45d2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.9673564 , 0.09820669, 0.80186037, 0.60490212, 0.58476881])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.set_state(state)\n",
    "np.random.random_sample(5)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
